package com.huhu.objects;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.huhu.manager.ResourcesManager;

public class Bullet extends Sprite {
	private boolean stopped = true;
	private PhysicsHandler physicsHandler;

	public Bullet(float pX, float pY, VertexBufferObjectManager vbom,
			Camera camera, PhysicsWorld physicsWorld) {
		super(pX, pY, ResourcesManager.getInstance().round_bullet_region, vbom);

		physicsHandler = new PhysicsHandler(this);
		registerUpdateHandler(physicsHandler);
	}

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		// TODO Auto-generated method stub
		super.onManagedUpdate(pSecondsElapsed);
		if (!stopped) {
			physicsHandler.setVelocity(0, physicsHandler.getVelocityY());
		} else {
//			physicsHandler.setVelocity(0, 0);
			setIgnoreUpdate(true);
		}
		if (getY() <= 0) {
			setIgnoreUpdate(true);
			setStopped(true);
		}
	}

	public void fire() {
		stopped = false;
		setVisible(true);
		physicsHandler.setVelocity(0, -500);
	}

	public void setStopped(boolean stopped) {
		this.stopped = stopped;
		if (stopped) {
			setVisible(false);
		} else {
			setVisible(true);
		}
	}
}
